package com.fau.hm.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Service;

import com.fau.hm.entity.MedicalResource;
import com.fau.hm.entity.MedicalRole;
import com.fau.hm.service.IUserManagerService;


@Service
public class MySecurityMetadataSource implements
		FilterInvocationSecurityMetadataSource {
	
	private IUserManagerService iUserManagerService;
	private Map<String, Collection<ConfigAttribute>> resourceMap = null;

	@Override
	public Collection<ConfigAttribute> getAllConfigAttributes() {
		return null;
	}
	
	@PostConstruct
	public void loadResourceDefine() {
		resourceMap = new HashMap<String, Collection<ConfigAttribute>>();
		List<MedicalResource> medicalResources = iUserManagerService.findAllResource();
		for(MedicalResource resource : medicalResources) {
			Collection<ConfigAttribute> configAttributes = new ArrayList<ConfigAttribute>();
			for(MedicalRole role : resource.getRoleSet()){
				ConfigAttribute ca = new SecurityConfig(role.getId());
				configAttributes.add(ca);
			}
			resourceMap.put(resource.getUrl(), configAttributes);
		}
		
		List<MedicalResource> rootResources = iUserManagerService.findRootResource();
		for(MedicalResource resource : rootResources) {
			Collection<ConfigAttribute> configAttributes = new ArrayList<ConfigAttribute>();
			for(MedicalRole role : resource.getRoleSet()){
				ConfigAttribute ca = new SecurityConfig(role.getId());
				configAttributes.add(ca);
			}
			resourceMap.put(resource.getUrl(), configAttributes);
		}
	}
	
	public void flush(){
		resourceMap = new HashMap<String, Collection<ConfigAttribute>>();
		List<MedicalResource> medicalResources = iUserManagerService.findAllResource();
		for(MedicalResource resource : medicalResources) {
			Collection<ConfigAttribute> configAttributes = new ArrayList<ConfigAttribute>();
			for(MedicalRole role : resource.getRoleSet()){
				ConfigAttribute ca = new SecurityConfig(role.getId());
				configAttributes.add(ca);
			}
			resourceMap.put(resource.getUrl(), configAttributes);
		}
		
		List<MedicalResource> rootResources = iUserManagerService.findRootResource();
		for(MedicalResource resource : rootResources) {
			Collection<ConfigAttribute> configAttributes = new ArrayList<ConfigAttribute>();
			for(MedicalRole role : resource.getRoleSet()){
				ConfigAttribute ca = new SecurityConfig(role.getId());
				configAttributes.add(ca);
			}
			resourceMap.put(resource.getUrl(), configAttributes);
		}
	}

	@Override
	public Collection<ConfigAttribute> getAttributes(Object arg0)
			throws IllegalArgumentException {
		String requestUrl = ((FilterInvocation) arg0).getRequestUrl();
		if(requestUrl.indexOf("?")>-1) requestUrl=requestUrl.substring(0,requestUrl.indexOf("?"));
		return resourceMap.get(requestUrl);
	}

	@Override
	public boolean supports(Class<?> arg0) {
		return true;
	}

	public IUserManagerService getiUserManagerService() {
		return iUserManagerService;
	}

	@Resource
	public void setiUserManagerService(IUserManagerService iUserManagerService) {
		this.iUserManagerService = iUserManagerService;
	}

}
